<?php

namespace Modules\Admin\Services\auth;

use Modules\Admin\Models\AuthGroup;
use Modules\Admin\Models\AuthRule;
use Modules\Admin\Services\BaseApiService;

class RuleService extends BaseApiService
{
    public function access(int $id){
        $authRuleArr = AuthRule::select('id','name','pid')->orderBy('sort','asc')->orderBy('id','asc')->get()->toArray();
        $defKeys = AuthGroup::where(['id'=>$id])->value('rules');
        if($defKeys){
            $defKeys = AuthRule::where(['type'=>3])->whereIn('id',explode("|", $defKeys))->pluck('id');
        }else{
            $defKeys = [];
        }
        return $this->apiSuccess('',[
            'ruleArr'=>$this->tree($authRuleArr),
            'defKeys'=>$defKeys,
            'id'=>$id
        ]);
    }
    public function index(){
        $model = AuthRule::query();
        $list = $model->orderBy('sort','asc')
            ->orderBy('id','desc')
            ->get()
            ->toArray();
        return $this->apiSuccess('',$this->tree($list));
    }
    public function store(array $data)
    {
        return $this->commonCreate(AuthRule::query(),$data);
    }
    public function edit(int $id){
        $data = AuthRule::find($id)->toArray();
        if($data['pid'] != 0){
            $data['value'] = $this->superiorArrId($data['pid']);
        }else{
            $data['value'] = [];
        }
        return $this->apiSuccess('',$data);
    }
    public function pidArr(int $pid){
        $value = [];
        if($pid != 0){
            $value = $this->superiorArrId($pid);
        }
        return $this->apiSuccess('',$value);
    }
    private function superiorArrId(int $pid):Array
    {
        $list = AuthRule::select('id','pid')->orderBy('id','asc')->get()->toArray();
        return array_reverse($this->superiorArrIdSort($list,$pid));
    }
    private function superiorArrIdSort(array $list,int $pid):Array
    {
        //创建新数组
        static $arr=array();
        foreach($list as $k=>$v){
            if($v['id'] == $pid){
                $arr[] = $v['id'];
                unset($list[$k]);
                $this->superiorArrIdSort($list,$v['pid']);
            }
        }
        return $arr;
    }
    public function update(int $id,array $data){
        return $this->commonUpdate(AuthRule::query(),$id,$data);
    }
    public function status(int $id,array $data){
        return $this->commonStatusUpdate(AuthRule::query(),$id,$data);
    }
    public function sorts(int $id,array $data){
        return $this->commonSortsUpdate(AuthRule::query(),$id,$data);
    }
    public function open(int $id,array $data){
        return $this->commonStatusUpdate(AuthRule::query(),$id,$data);
    }
    public function affix(int $id,array $data){
        return $this->commonStatusUpdate(AuthRule::query(),$id,$data);
    }
    public function cDestroy(int $id){
        $idArr = $this->ids($id);
        return $this->commonDestroy(AuthRule::query(),$idArr);
    }
    private function ids(int $id):Array
    {
        $rule = AuthRule::select('id','pid')->get()->toArray();
        $arr = $this->delSort($rule,$id);
        $arr[] = $id;
        return $arr;
    }
    public function delSort(array $rule,int $id):Array
    {
        //创建新数组
        static $arr=array();
        foreach($rule as $k=>$v){
            if($v['pid'] == $id){
                $arr[] = $v['id'];
                unset($rule[$k]);
                $this->delSort($rule,$v['id']);
            }
        }
        return $arr;
    }
}

